/*
 * Records Package
 */
package edu.gatech.oad.records;

import edu.gatech.oad.doctorsorder.LabOrder;
import edu.gatech.oad.user.Patient;

/**
 * Manages the interaction with the TreatmentRecords
 * @author Nathan Hurley
 * @version 1.0
 *
 */
public class TreatmentRecordController {
	/**
	 * The record to add and remove orders from
	 */
	private TreatmentRecord curRecord;
	
	/**
	 * Constructs a controller to manage a TreatmentRecord
	 * @param curRecord  TreatmentRecord to manipulate
	 */
	public TreatmentRecordController(TreatmentRecord curRecord){
		this.curRecord = curRecord;
	}
	
	/**
	 * @return  the current TreatmentRecord
	 */
	public TreatmentRecord getCurRecord(){
		return curRecord;
	}
	
	/**
	 * @param curRecord  the new current TreatmentRecord
	 */
	public void setCurRecord(TreatmentRecord curRecord){
		this.curRecord = curRecord;
	}
	
	/**
	 * Saves the order by adding it to the PatientHistory and 
	 * to the current TreatmentRecord
	 * @param newOrder the LabOrder to add to the PatientHistory
	 */
	public void saveOrder(LabOrder newOrder){
		final PatientHistory thisHistory = getPatient().getPatientHistory();
		final LabResults newResults = new LabResults("", newOrder);
		newOrder.setLabResults(newResults);
		thisHistory.addLabRecord(newResults);
		curRecord.addDoctorsOrder(newOrder);
	}
	
	/**
	 * Removes a prescription or LabOrder from the TreatmentRecord and
	 * the PatientHistory if the DoctorsOrder is a LabOrder
	 * @param indexToRemove index of record to remove
	 */
	public void removeOrder(int indexToRemove){
		final PatientHistory thisHistory = getPatient().getPatientHistory();
		if(curRecord.getDoctorsOrders().get(indexToRemove) instanceof LabOrder){
			thisHistory.removeLabRecord(((LabOrder) 
					curRecord.getDoctorsOrders().get(indexToRemove)).getLabResult());
		}
		curRecord.getDoctorsOrders().remove(indexToRemove);
	}
	
	/**
	 * @return the Patient for this controller
	 */
	public Patient getPatient(){
		return curRecord.getAppointment().getPatient();
	}
	
	/**
	 * @return String representing the controller with the given 
	 * TreatmentRecord
	 */
	public String toString(){
		return "TreatmentRecordController for the TreatmentRecord: "
			+ curRecord.toString();
	}
}
